package com.zhan.ielts.web.controller.speakingmodify;

import com.zhan.frame.core.dao.BaseService;
import com.zhan.frame.core.utils.BeanUtils;
import com.zhan.frame.core.utils.NetUtil;
import com.zhan.frame.core.web.controller.BaseControllerImpl;
import com.zhan.frame.core.web.domain.Result;

import com.zhan.ielts.domain.comment.Comment;
import com.zhan.ielts.domain.speakingmodify.SpeakingModifyAudio;
import com.zhan.ielts.service.comment.ICommentService;
import com.zhan.ielts.service.speakingmodify.ISpeakingModifyAudioService;
import com.zhan.ielts.utils.CustomPropertisUtils;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by tlfu on 2017/1/11.
 */
@Controller
@RequestMapping("/speakingmodify/audio")
public class SpeakingModifyAudioController extends BaseControllerImpl<SpeakingModifyAudio, SpeakingModifyAudio> {
    @Autowired
    private ISpeakingModifyAudioService speakingModifyAudioService;
    @Autowired
    private ICommentService commentService;

    @Override
    protected BaseService<SpeakingModifyAudio> getBaseService() {
        return speakingModifyAudioService;
    }

    @RequestMapping(value = "/selectPageList", method = RequestMethod.GET)
    public ModelAndView selectList(SpeakingModifyAudio query, String isComment, @PageableDefault Pageable pageable) {
        Boolean is = null;
        if ("true".equals(isComment)) {
            is = true;
        } else if ("false".equals(isComment)) {
            is = false;
        }
        if (!StringUtils.isEmpty(query.getSource())) {
            if (query.getSource().equals("all")) {
                query.setSource(null);
            } else if (query.getSource().equals("free")) {
                query.setSource("");
            }

        }
        Page<SpeakingModifyAudio> page = speakingModifyAudioService.selectPageList(is, query, pageable);
        ModelAndView mav = new ModelAndView("/speakingmodify/audio/listAudio", "page", page);
        Map map = BeanUtils.toMap(query);
        if (null == query.getSource()) {
            map.put("source","all");
        } else if ("".equals(query.getSource())) {
            map.put("source","free");
        }
        map.put("isComment", isComment);
        mav.addObject("query", map);
        return mav;
    }

    @RequestMapping(value = "/editEntity", method = RequestMethod.GET)
    public ModelAndView editView(String commentId, String id) {
        Comment result = commentService.queryById(commentId);
        SpeakingModifyAudio audio = speakingModifyAudioService.queryById(id);
        Map map = new HashMap();
        if (result != null) {
            map = BeanUtils.toMap(result);
        }
        map.put("audioId", id);
        map.put("userId", audio.getUserId());
        return new ModelAndView("/speakingmodify/audio/editAudio", map);
    }

    @ResponseBody
    @RequestMapping(value = "/editEntity", method = RequestMethod.POST)
    public Result addEntity(Comment comment, String resourceId, String userId) {
        try {
            comment.setCommentModule(1);
            comment.setCommentType(1);
            comment.setResourceId(resourceId);
            comment.setCreateTime(new Date());
            comment.setUpdateTime(comment.getCreateTime());
            boolean isAdd = StringUtils.isEmpty(comment.getId());
            commentService.insert(comment);
            speakingModifyAudioService.updateCorrectStatus(resourceId,1);
            if (isAdd) {
                pushMessage(userId, resourceId);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(Result.Status.ERROR, e.getMessage());
        }
        return new Result(Result.Status.OK, "success");
    }

    private void pushMessage(String userId, String resourceId) {
        try {
            SpeakingModifyAudio audio = speakingModifyAudioService.queryById(resourceId);
            Map<String, Object> map = new HashMap<>();
            map.put("bankType", "4");
            map.put("type","3");
            map.put("content", "你提交的口语录音老师已经批改好啦~");
            map.put("url", CustomPropertisUtils.getProperties("speakingModifyUrl") + "&questionId=" + audio.getQuestionId() + "&userId=" + userId);
            map.put("title", "小站雅思");
            String content = JSONObject.fromObject(map).toString();
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("content", content);
            paramMap.put("appName", "ielts");
            paramMap.put("deviceName", "all");
            paramMap.put("userIds", userId);
            String pushUrl = CustomPropertisUtils.getProperties("commonUrl") + "/push/pushMessage";
             String result=  NetUtil.doPost(pushUrl, JSONObject.fromObject(paramMap).toString(), true);
            System.out.println(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ResponseBody
    @RequestMapping(value = "/deleteEntity", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE)
    public Result deleteOne(String id, String commentId) {
        if (org.apache.commons.lang3.StringUtils.isBlank(id)) {
            return new Result(Result.Status.ERROR, "没有传入要删除的ID号！");
        }
        if (!StringUtils.isEmpty(commentId)) {
            commentService.deleteById(commentId);
        }
        int count = getBaseService().deleteById(id);
        return new Result(Result.Status.OK, count);
    }
}
